/* 
封装使用axios发请求的自定义hook
*/
import axios from 'axios'
import {
  ref
} from 'vue'

export default function useUrlLoader<T>(url: string) {
  const loading = ref(false)
  const result = ref<T>()
  const errorMsg = ref<String>()

  loading.value = true
  // 发送请求
  axios.get(url).then(response => {
    loading.value = false
    result.value = response.data
  }).catch(error => {
    loading.value = false
    errorMsg.value = '请求失败: ' + error.message
  })

  return {
    loading,
    result,
    errorMsg
  }
}